package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.enums.RobotModelEnum;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotConfig;
import com.hitqz.robot.biz.mapper.RobotConfigMapper;
import com.hitqz.robot.biz.service.RobotConfigService;
import com.hitqz.robot.biz.service.RobotService;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

/**
 * @author xupkun
 * @date 2024/8/1
 */
@Service
@AllArgsConstructor
public class RobotConfigServiceImpl extends ServiceImpl<RobotConfigMapper, RobotConfig> implements RobotConfigService {

    final private ServerProperties serverProperties;

    final private RobotService robotService;

    private static String robotIcon = "AGV1.png";


    @PostConstruct
    private void init() {
        RobotConfig robotConfig = this.getOneEnable();
        if (robotConfig == null) {
            return;
        }
        String robotModelUri = robotConfig.getRobotModelUri();
        if (StrUtil.isNotBlank(robotModelUri)){
            robotIcon = robotModelUri;
        }
    }



    @Override
    public RobotConfig getOneEnable() {
        RobotConfig robotConfig = baseMapper.selectOne(Wrappers.<RobotConfig>lambdaQuery()
                .eq(RobotConfig::getEnable, 1));
        if (robotConfig == null) {
            robotConfig = new RobotConfig();
            robotConfig.setRobotModelName(RobotModelEnum.AGV.getFlag());
            robotConfig.setEnable(1);
            robotConfig.setProcessManagerUri("http://" + this.findMatchingLocalIp() + ":" + serverProperties.getPort());
            this.save(robotConfig);
        }
        return robotConfig;
    }

    @Override
    public String getRobotIcon() {
        return robotIcon;
    }

    @Override
    public void setRobotIcon(String icon) {
        robotIcon = icon;
    }


    private String findMatchingLocalIp() {
        Robot defaultRobot = robotService.getDefaultRobot();
        String targetIp = defaultRobot.getServerAddress();
        if (targetIp == null) {
            targetIp = "127.0.0.1";
        }
        // 获取所有的网络接口
        Enumeration<NetworkInterface> networkInterfaces = null;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                if (inetAddress instanceof java.net.Inet4Address) {
                    String localIp = inetAddress.getHostAddress();
                    // 检查本机IP是否与目标IP相同
                    if (localIp.equals(targetIp)) {
                        return localIp;
                    }
                }
            }
        }
        return "127.0.0.1";
    }

}
